﻿<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:in="http://www.composite.net/ns/transformation/input/1.0" xmlns:lang="http://www.composite.net/ns/localization/1.0" xmlns:f="http://www.composite.net/ns/function/1.0" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xsl in lang f" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:nodar="urn:NodAr">
	<xsl:param name="interval" select="/in:inputs/in:param[@name='Interval']" />
	<xsl:param name="showcontrols" select="/in:inputs/in:param[@name='ShowControls']" />
	<xsl:param name="showindicators" select="/in:inputs/in:param[@name='ShowIndicators']" />
	<xsl:param name="cssclass" select="/in:inputs/in:param[@name='CssClass']" />
	<msxsl:script language="VB" implements-prefix="nodar">
		<msxsl:assembly name="System.Net" />
		<msxsl:using namespace="System.Net" /><![CDATA[
		Dim mCounter as Integer=0
		Dim mSliderCounter as Integer=-1
		Dim mIndicatorCounter as Integer=0

		Public Function GetCarouselID() As String
			Dim strId as String
			strId="carousel-" & Guid.NewGuid.ToString.Replace("-","").Substring(1, 5)
			Return strId
		End Function		
		
		Public Function ItemClass() As String						
			mCounter+=1
			If mCounter=1 Then
				Return "item active"
			Else
				Return "item"
			End IF			
		End Function

		Public Function GetCarouselSlidersID() As String
			mSliderCounter+=1
			Return mSliderCounter.ToString
		End Function

		Public Function IsIndicatorFirst() As Boolean
			mIndicatorCounter+=1
			If mIndicatorCounter=1 Then
				Return True
			Else
				Return False
			End If
		End Function

		Public Function DecodeTags(str As String) As String
			'Return WebUtility.HtmlDecode(str)
Return "<h1>Test</>"
		End Function
	]]></msxsl:script>
	<xsl:template match="/">
		<html>
			<head>
				<!-- markup placed here will be shown in the head section of the rendered page -->
			</head>
			<body>
				<xsl:variable name="CarouselId" select="nodar:GetCarouselID()" />
				<div class="" data-ride="carousel">
					<xsl:attribute name="id">
						<xsl:value-of select="$CarouselId" />
					</xsl:attribute>
					<xsl:attribute name="class">
						<xsl:text>carousel slide </xsl:text>
						<xsl:value-of select="$cssclass" />
					</xsl:attribute>
					<xsl:attribute name="data-interval">
						<xsl:value-of select="$interval" />
					</xsl:attribute>
					<xsl:if test="$showindicators='true'">
						<ol class="carousel-indicators">
							<xsl:for-each select="/in:inputs/in:result[@name='GetCarouselItemXml']/CarouselItem">
								<li>
									<xsl:attribute name="data-target">
										<xsl:text>#</xsl:text>
										<xsl:value-of select="$CarouselId" />
									</xsl:attribute>
									<xsl:attribute name="data-slide-to">
										<xsl:value-of select="nodar:GetCarouselSlidersID()" />
									</xsl:attribute>
									<xsl:if test="nodar:IsIndicatorFirst()='true'">
										<xsl:attribute name="class">active</xsl:attribute>
									</xsl:if>
								</li>
							</xsl:for-each>
						</ol>
					</xsl:if>
					<div class="carousel-inner">
						<xsl:for-each select="/in:inputs/in:result[@name='GetCarouselItemXml']/CarouselItem">
							<div>
								<xsl:attribute name="class">
									<xsl:value-of select="nodar:ItemClass()" />
								</xsl:attribute>
								<xsl:choose>
									<xsl:when test="@URL !=''">
										<xsl:apply-templates mode="LinkedImage" select="." />
									</xsl:when>
									<xsl:otherwise>
										<xsl:apply-templates mode="SimpleImage" select="." />
									</xsl:otherwise>
								</xsl:choose>
								<xsl:if test="@Caption !=''">
									<div class="carousel-caption">
										<xsl:copy-of select="Parser:ParseXhtmlBodyFragment(@Caption)" xmlns:Parser="#MarkupParserExtensions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />
									</div>
								</xsl:if>
							</div>
						</xsl:for-each>
					</div>
					<xsl:if test="$showcontrols='true'">
						<a class="left carousel-control" href="" role="button" data-slide="prev">
							<xsl:attribute name="href">
								<xsl:text>#</xsl:text>
								<xsl:value-of select="$CarouselId" />
							</xsl:attribute>
							<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
							<span class="sr-only">Previous</span>
						</a>
						<a class="right carousel-control" role="button" data-slide="next">
							<xsl:attribute name="href">
								<xsl:text>#</xsl:text>
								<xsl:value-of select="$CarouselId" />
							</xsl:attribute>
							<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
							<span class="sr-only">Next</span>
						</a>
					</xsl:if>
				</div>
			</body>
		</html>
	</xsl:template>
	<xsl:template mode="LinkedImage" match="*">
		<a href="{@URL}">
			<img src="~/media({@Image.KeyPath})" alt="{@Image.Title}" />
		</a>
	</xsl:template>
	<xsl:template mode="SimpleImage" match="*">
		<img src="~/media({@Image.KeyPath})" alt="{@Image.Title}" />
	</xsl:template>
</xsl:stylesheet>